package v10

type User struct {
	Name        string         `v:"required,alphaunicode"`
	Age         uint8          `v:"gte=10,lte=130"`
	Phone       string         `v:"required,e164"`
	Email       string         `v:"required,email"`
	FColor      string         `v:"iscolor"`
	FColor1     string         `v:"hexcolor|rgb|rgba|hsl|hsla"`
	Address     *Address       `v:"required"`
	ContactUser []*ContactUser `v:"required,gte=1,dive"` // dive 深入到下一层
	// dive：进入下一层校验，也就是针对切片的各个值进行校验
	// dive后的内容：required,gte=2,alphaunicode，这个是对切片每个值的校验规则
	Hobby []string `v:"required,gte=2,dive,required,gte=2,alphaunicode"`
}

type ContactUser struct {
	Name    string   `v:"required,alphaunicode"`
	Phone   string   `v:"required_without_all=Email Address,omitempty,e164"` // omitempty 忽略空串
	Email   string   `v:"required_without_all=Phone Address,omitempty,email"`
	Address *Address `v:"required_without_all=Phone Email"`
}

type Address struct {
	Province string `v:"required"`
	City     string `v:"required"`
}

func StructValidate() {
	address := Address{
		Province: "GD",
		City:     "GZ",
	}

	cu1 := &ContactUser{
		Name:  "张三",
		Phone: "+8613800138000",
		Email: "",
	}

	cu2 := &ContactUser{
		Name:  "张三",
		Phone: "",
		Email: "nick@example.com",
	}

	u := &User{
		Name:        "nick",
		Age:         18,
		Phone:       "+8613800138000",
		Email:       "nick@example.com",
		FColor:      "#ffffff",
		FColor1:     "rgb(255, 255, 255)",
		Address:     &address,
		ContactUser: []*ContactUser{cu1, cu2},
		Hobby:       []string{"ab", "ba"},
	}

	v := _validate
	err := v.Struct(u)
	if err != nil {
		outRes("struct", err)
	}
}

// MapValidate map 校验
func MapValidate() {
	var err error
	v := _validate
	mp := map[string]string{
		"A": "123456",
		"B": "123456",
		"C": "123456",
	}
	// 校验规则拆分：gte=3,dive,keys,len=1,alpha,endkeys,required,gte=5,lte=10,number
	// gte: map 的长度校验
	// dive: 后面的规则是进入 map 内容校验，也就是针对key-value进行校验
	// keys ... endkeys: 对 map 的 key 进行校验，
	// endkeys 后: 对 map 的 value 进行校验
	err = v.Var(mp, "gte=3,dive,keys,len=1,alpha,endkeys,required,gte=5,lte=10,number")
	outRes("mp", err)
}
